package fun.qu_an.minecraft.mini_games_helper.client.mixin;

import fun.qu_an.minecraft.mini_games_helper.client.handle.ForceOptionHelperSodium;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.hud.PlayerListHud;
import net.minecraft.client.network.PlayerListEntry;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.world.GameMode;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(PlayerListHud.class)
public class PlayerListHudMixin {
	@Shadow
	@Final
	private MinecraftClient client;

	@Inject(method = "applyGameModeFormatting(Lnet/minecraft/client/network/PlayerListEntry;Lnet/minecraft/text/MutableText;)Lnet/minecraft/text/Text;",
		at = @At(value = "HEAD"), cancellable = true)
	private void onApplyGameModeFormatting(PlayerListEntry entry, MutableText name, CallbackInfoReturnable<Text> cir) {
		if (client.interactionManager == null) {
			return;
		}
		if (!ForceOptionHelperSodium.isHidePlayerList(client.interactionManager.getCurrentGameMode())) {
			return;
		}
		cir.setReturnValue(name);
	}

	@Redirect(method = "render(Lnet/minecraft/client/gui/DrawContext;ILnet/minecraft/scoreboard/Scoreboard;Lnet/minecraft/scoreboard/ScoreboardObjective;)V",
		at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/PlayerListEntry;getGameMode()Lnet/minecraft/world/GameMode;"))
	private GameMode onRender(PlayerListEntry instance) {
		GameMode gameMode = instance.getGameMode();
		if (client.interactionManager == null) {
			return gameMode;
		}
		if (!ForceOptionHelperSodium.isHidePlayerList(client.interactionManager.getCurrentGameMode())) {
			return gameMode;
		}
		return gameMode == GameMode.SPECTATOR ? GameMode.CREATIVE : gameMode;
	}
}
